﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Collections;
using System.Configuration;
using System.Net.NetworkInformation;

using ECS.Framework;
using ECS.BLL.Component;
using ECS.BLL.Entity;

namespace E_ICS.E_ICS
{
    public partial class frmWeightSlipDisplay : Form
    {
        #region private properties
        SearchInfo searchData = null;

        Main.frmMain2 main;
        DataSet dtWeightSlip = null;
        DataTable MAWB_TB;
        //SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["con3"].ConnectionString);
        //string appId = ConfigurationManager.AppSettings["appID"];
        //string device = ConfigurationManager.AppSettings["device"];
        string MAWB_id, slip_id, MAWB, HAWB, awb_seq, awb_move_seq, partial_seq, Piece, net, carrier, flight_number, flight_date, destination, NOG, scale_id, slip_number, TWSC, agent_code;
        string newdate;
        string stvalidationMMHH = "PASS";
        string comboboxst = "";
        DataTable TW = new DataTable();
        int index = 0;
        #endregion

        #region form events

        public frmWeightSlipDisplay(Main.frmMain2 _main)
        {
            try
            {
                InitializeComponent();
                main = _main;
                //if (ViewEditWeightSlip.flagfromViewEditWeightSlip == -1)
                //{
                //    SetfrmWeightFormmain();
                //}
                //else
                //{
                //    if (ViewEditWHInventory.MAWB_ID.Equals(string.Empty))
                //    {
                //        comboBox1.SelectedIndex = 0;
                //    }
                //    else
                //    {
                //        SetfrmWeightSlipDisplay();
                //    }
                //}

            }
            catch
            {

                //throw;
            }

        }

        private void frmWeightSlipDisplay_Load(object sender, EventArgs e)
        {
            try
            {
                //-- START add 2015.04.23 c
                if (ViewEditWeightSlip.flagfromViewEditWeightSlip == -1)
                {
                    SetfrmWeightFormmain();
                }
                else
                {
                    if (ViewEditWHInventory.MAWB_ID.Equals(string.Empty))
                    {
                        cboConsolType.SelectedIndex = 0;
                    }
                    else
                    {
                        SetfrmWeightSlipDisplay();
                    }
                }
                //-- END add 2015.04.23 c

                this.toolStripStatusLabel2.Text = GlobalVariable.userName;


                dpkFromDate.Enabled = false;
                dpkToDate.Enabled = false;
                txtDateTo.Enabled = false;
                txtDateFrom.Enabled = false;
                imbCD.Enabled = false;
                imbPD.Enabled = false;
                imbPH.Enabled = false;
                dgvWeightSlipDisplay.RowHeadersWidth = 80;
                txtdatetime.Text = "";
                searchData = new SearchInfo();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                InsearchValueEditMAAWBInfo();
                E_ICS.frmEditMAWBInfo emi = new E_ICS.frmEditMAWBInfo(main);
                if (emi.ShowDialog() == DialogResult.Yes)
                {
                    if (ViewEditWeightSlip.flagUsedateSearch)
                    {
                        SearchWeightSlipwithdate();
                    }
                    else
                    {
                        SearchWeightSlip();
                    }


                }
            }
            catch
            {

                //throw;
            }

        }

        private void btnAgentSearch_Click(object sender, EventArgs e)
        {
            try
            {
                frmAgentSearch a = new frmAgentSearch(main);
                if (a.ShowDialog() == DialogResult.Yes)
                {

                    txtAgentCode.Text = ValueTofrmAddMAWB.SelAgentCode;

                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                checkcombobox();
                ClearViewInventoryValue();
                DateTime date;
                if (!string.IsNullOrEmpty(txtdatetime.Text))
                {
                    if (DateTime.TryParse(txtdatetime.Text, out date) == false)
                    {
                        CommonMessageBox.ShowMessageWarning("Flightdate format is not correct.");
                        txtdatetime.Focus();
                        return;
                    }
                }
                if (chkDataSearch.Checked)
                {
                    validationMMHH(txtDateFrom);
                    validationMMHH(txtDateTo);
                    if (stvalidationMMHH.Equals("PASS"))
                    {
                        SearchWeightSlipwithdate();
                        setRowNumber(dgvWeightSlipDisplay);
                    }
                    else

                        //คืนค่า
                    {
                        stvalidationMMHH = "PASS";
                    }
                }
                else
                {
                    SearchWeightSlip();
                    setRowNumber(dgvWeightSlipDisplay);
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void chkDataSearch_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (chkDataSearch.Checked)
                {
                    dpkFromDate.Enabled = true;
                    dpkToDate.Enabled = true;
                    txtDateTo.Enabled = true;
                    txtDateFrom.Enabled = true;
                    imbCD.Enabled = true;
                    imbPD.Enabled = true;
                    imbPH.Enabled = true;
                    ViewEditWeightSlip.flagUsedateSearch = true;

                }

                else
                {
                    dpkFromDate.Enabled = false;
                    dpkToDate.Enabled = false;
                    txtDateTo.Enabled = false;
                    txtDateFrom.Enabled = false;
                    imbCD.Enabled = false;
                    imbPD.Enabled = false;
                    imbPH.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void imbPD_Click(object sender, EventArgs e)
        {
            try
            {
                set_PDtime_to_dpk();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void imbCD_Click(object sender, EventArgs e)
        {
            try
            {
                set_CDtime_to_dpk();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void imbPH_Click(object sender, EventArgs e)
        {
            try
            {
                set_PHtime_to_dpk();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            try
            {
                cboConsolType.Text = "ALL";
                txtAgentCode.Text = "";
                txtCarrier.Text = "";
                txtDateFrom.Text = "00:00";
                txtDateTo.Text = "23:59";
                txtFltNo.Text = "";
                txtHAWB.Text = "";
                txtPrefix.Text = "";
                txtScaleID.Text = "";
                txtSerial.Text = "";
                txtSlipNumber.Text = "";
                chkDataSearch.Checked = false;
                dgvWeightSlipDisplay.DataSource = null;
                //dpkFlightDate.Value = DateTime.Now;
                dpkFromDate.Value = DateTime.Now;
                dpkToDate.Value = DateTime.Now;
                dpkFlightDate.Value = DateTime.Now;
                ClearViewInventoryValue();
                txtdatetime.Text = "";
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void dgvWeightSlipDisplay_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                index = e.RowIndex;
                if (e.RowIndex >= 0)
                {


                    dgvWeightSlipDisplay.Rows[index].Selected = true;
                    if (dgvWeightSlipDisplay.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                    {
                        ViewEditWeightSlip.MAWB_ID = dgvWeightSlipDisplay.Rows[e.RowIndex].Cells[0].Value.ToString();
                        ViewEditWeightSlip.MAWB = dtWeightSlip.Tables[0].Rows[index]["display_mawb"].ToString().Trim(); ;
                        ViewEditWeightSlip.slip_id = dgvWeightSlipDisplay.Rows[e.RowIndex].Cells[1].Value.ToString();
                        ViewEditWeightSlip.awb_seq = dgvWeightSlipDisplay.Rows[e.RowIndex].Cells[4].Value.ToString();
                        ViewEditWeightSlip.awb_move_seq = dgvWeightSlipDisplay.Rows[e.RowIndex].Cells[5].Value.ToString();
                        ViewEditWeightSlip.partial_seq = dgvWeightSlipDisplay.Rows[e.RowIndex].Cells[6].Value.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void dgvWeightSlipDisplay_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                index = e.RowIndex;
                if (e.RowIndex >= 0)
                {


                    dgvWeightSlipDisplay.Rows[index].Selected = true;
                    if (dgvWeightSlipDisplay.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                    {

                        dgvWeightSlipDisplay.ClearSelection();


                        ViewEditWeightSlip.MAWB_ID = "";
                        ViewEditWeightSlip.slip_id = "";
                        ViewEditWeightSlip.awb_seq = "";
                        ViewEditWeightSlip.awb_move_seq = "";
                        ViewEditWeightSlip.partial_seq = "";


                    }

                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void dpkFlightDate_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                txtdatetime.Text = String.Format("{0:ddMMMyy}", dpkFlightDate.Value).ToUpper();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }

        private void dgvWeightSlipDisplay_Sorted(object sender, EventArgs e)
        {
            try
            {
                setRowNumber(dgvWeightSlipDisplay);
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        #endregion

        #region private methods

        private void SetfrmWeightFormmain()
        {
            txtPrefix.Text = ViewEditWeightSlip.MAWB.Substring(0, 3);
            txtSerial.Text = ViewEditWeightSlip.MAWB.Substring(3, ViewEditWeightSlip.MAWB.Length - txtPrefix.Text.Length - 1);

            txtHAWB.Text = ViewEditWeightSlip.HAWB;
            txtCarrier.Text = ViewEditWeightSlip.Carrier;
            txtFltNo.Text = ViewEditWeightSlip.Flight_No;
            dpkFlightDate.Value = Convert.ToDateTime(ViewEditWeightSlip.Flight_Date);
            if (ViewEditWeightSlip.HAWB.Equals(string.Empty))
            {
                cboConsolType.SelectedIndex = 2;
            }
            else
            {
                cboConsolType.SelectedIndex = 1;
            }
            // txtScaleID.Text = ViewEditWeightSlip.Scale_ID;
            //  txtSlipNumber.Text = ViewEditWHInventory.slip_id;
        }

        private void InsearchValueEditMAAWBInfo()
        {
            ViewEditWeightSlip.MAWB_ID = TW.Rows[index]["MAWB_ID"].ToString();
            ViewEditWeightSlip.slip_id = TW.Rows[index]["slip_id"].ToString();
            ViewEditWeightSlip.MAWB = TW.Rows[index]["MAWB"].ToString();
            ViewEditWeightSlip.HAWB = TW.Rows[index]["HAWB"].ToString();
            ViewEditWeightSlip.Piece = TW.Rows[index]["Piece"].ToString();
            ViewEditWeightSlip.Weight = TW.Rows[index]["Weight"].ToString();
            ViewEditWeightSlip.Carrier = TW.Rows[index]["Carrier"].ToString();
            ViewEditWeightSlip.Flight_No = TW.Rows[index]["Flight No"].ToString();
            ViewEditWeightSlip.Flight_Date = TW.Rows[index]["Flight Date"].ToString();
            ViewEditWeightSlip.Destination = TW.Rows[index]["Destination"].ToString();
            ViewEditWeightSlip.Nature_of_Goods = TW.Rows[index]["Nature of Goods"].ToString();
            ViewEditWeightSlip.Scale_ID = TW.Rows[index]["Scale ID"].ToString();
            ViewEditWeightSlip.Slip_Number = TW.Rows[index]["Slip Number"].ToString();
            ViewEditWeightSlip.WS_Create = TW.Rows[index]["WS Create"].ToString();
            ViewEditWeightSlip.Agent = TW.Rows[index]["Agent"].ToString();
            ViewEditWeightSlip.Agent_Name = dtWeightSlip.Tables[0].Rows[0]["agent_name"].ToString().Trim();


        }

        private void SetfrmWeightSlipDisplay()
        {
            txtPrefix.Text = ViewEditWHInventory.MAWB.Substring(0, 3);
            txtSerial.Text = ViewEditWHInventory.MAWB.Substring(4, ViewEditWHInventory.MAWB.Length - txtPrefix.Text.Length - 1);
            txtHAWB.Text = ViewEditWHInventory.HAWB;
            txtCarrier.Text = ViewEditWHInventory.Flight_No.Substring(0, 2);
            txtFltNo.Text = ViewEditWHInventory.Flight_No.Substring(2, ViewEditWHInventory.Flight_No.Length - txtCarrier.Text.Length);
            dpkFlightDate.Value = Convert.ToDateTime(ViewEditWHInventory.Flight_Date);
            if (ViewEditWHInventory.HAWB.Equals(string.Empty))
            {
                cboConsolType.SelectedIndex = 2;
            }
            else
            {
                cboConsolType.SelectedIndex = 1;
            }
            txtScaleID.Text = ViewEditWHInventory.scale_id;
            txtSlipNumber.Text = ViewEditWHInventory.slip_id;
        }

        private void ClearViewInventoryValue()
        {
            ViewEditWHInventory.MAWB_ID = "";
            ViewEditWHInventory.slip_id = "";
            ViewEditWHInventory.MAWB = "";
            ViewEditWHInventory.HAWB = "";
            ViewEditWHInventory.Flight_No = "";
            ViewEditWHInventory.Flight_Date = "";
            ViewEditWHInventory.Partial_Acc = "";
            ViewEditWHInventory.Piece = "";
            ViewEditWHInventory.Weight = "";
            ViewEditWHInventory.Last_Stored = "";
            ViewEditWHInventory.Last_Retrieved = "";
            ViewEditWHInventory.Status = "";
            ViewEditWHInventory.scale_id = "";

        }

        private void SearchWeightSlip()
        {
            //SqlCommand cmd = new SqlCommand();

            //// Int32 rowsAffected;

            //cmd.CommandText = "usp_SearchWeightSlip";
            //cmd.CommandType = CommandType.StoredProcedure;
            //cmd.Connection = sqlCon;
            //cmd.Parameters.AddWithValue("@in_vchApplicationID", appId);
            //if (txtdatetime.Text.Equals(string.Empty))
            //{
            //    cmd.Parameters.AddWithValue("@in_dtFlightDate", null);
            //}
            //else
            //{
            //    cmd.Parameters.AddWithValue("@in_dtFlightDate", dpkFlightDate.Value);
            //}

            //cmd.Parameters.AddWithValue("@in_vchDevice", device);
            //cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.userName);
            //cmd.Parameters.AddWithValue("@in_vchMAWB", txtPrefix.Text + "-" + txtSerial.Text);
            //cmd.Parameters.AddWithValue("@in_vchMAWBPrefix", "%" + txtPrefix.Text + "%");
            //cmd.Parameters.AddWithValue("@in_vchMAWBSerial", "%" + txtSerial.Text + "%");
            //cmd.Parameters.AddWithValue("@in_vchHAWB", "%" + txtHAWB.Text + "%");
            //cmd.Parameters.AddWithValue("@in_vchCarrier", "%" + txtCarrier.Text + "%");
            //cmd.Parameters.AddWithValue("@in_vchFlightNumber", "%" + txtFltNo.Text + "%");
            //cmd.Parameters.AddWithValue("@in_vchAgentCode", "%" + txtAgentCode.Text);
            //cmd.Parameters.AddWithValue("@in_vchScaleID", "%" + txtScaleID.Text);
            //cmd.Parameters.AddWithValue("@in_vchSlipNumber", "%" + txtSlipNumber.Text + "%");
            //cmd.Parameters.AddWithValue("@in_vchShipmentype", comboboxst);

            
            SearchCriteria criteria = new SearchCriteria();
            criteria.AppId = GlobalVariable.user.ApplicationID;
            criteria.Device = GlobalVariable.Device;
            criteria.UserId = GlobalVariable.user.UserID;
            if (!string.IsNullOrEmpty(txtdatetime.Text))
            {
                
                criteria.FlightDate = Convert.ToDateTime(txtdatetime.Text);
            }
            criteria.MAWB = txtPrefix.Text + "-" + txtSerial.Text;
            criteria.MAWBPrefix = string.Format("%{0}%", txtPrefix.Text);
            criteria.MAWBSerial = string.Format("%{0}%", txtSerial.Text);
            criteria.HAWB = string.Format("%{0}%", txtHAWB.Text);
            criteria.Carrier = string.Format("%{0}%", txtCarrier.Text);
            criteria.FlightNo = string.Format("%{0}%", txtFltNo.Text);
            criteria.AgentCode = string.Format("%{0}", txtAgentCode.Text);
            criteria.ScaleId = string.Format("%{0}", txtScaleID.Text);
            criteria.SlipNo = string.Format("%{0}%", txtSlipNumber.Text);
            criteria.SipmentType = comboboxst;

            dtWeightSlip = searchData.SearchWeightSlip(criteria);

            //sqlCon.Open();

            //SqlDataAdapter da = new SqlDataAdapter(cmd);
            //dtWeightSlip = new DataSet();
            //da.Fill(dtWeightSlip, "WeightSlip");
            MAWB_TB = new DataTable();
            MAWB_TB = CreateWeightSlipDisplay_TB(dtWeightSlip);
            dgvWeightSlipDisplay.DataSource = MAWB_TB;
            //dgvWeightSlipDisplay.Columns[0].Visible = false;
            //dgvWeightSlipDisplay.Columns[1].Visible = false;
            setRowNumber(dgvWeightSlipDisplay);
            if (dgvWeightSlipDisplay.RowCount > 0)
            {
                setdgvdisplay();
                dgvWeightSlipDisplay.Rows[0].Selected = true;
            }
            else
            {
                CommonMessageBox.ShowMessageWarning("Data not found");
            }
            //sqlCon.Close();
        }

        private DataTable CreateWeightSlipDisplay_TB(DataSet ds)
        {
            TW = new DataTable();
            ///////////create head column/////////////////

            TW.Columns.Add("MAWB_ID", typeof(string));
            TW.Columns.Add("slip_id", typeof(string));
            TW.Columns.Add("MAWB", typeof(string));
            TW.Columns.Add("HAWB", typeof(string));
            TW.Columns.Add("AWB Sequence", typeof(string));
            TW.Columns.Add("AWB Move Sequence", typeof(string));
            TW.Columns.Add("MAWB Partial Sequence", typeof(string));
            TW.Columns.Add("Piece", typeof(string));
            TW.Columns.Add("Weight", typeof(string));
            TW.Columns.Add("Carrier", typeof(string));
            TW.Columns.Add("Flight No", typeof(string));
            TW.Columns.Add("Flight Date", typeof(string));
            TW.Columns.Add("Destination", typeof(string));
            TW.Columns.Add("Nature of Goods", typeof(string));
            TW.Columns.Add("Scale ID", typeof(string));
            TW.Columns.Add("Slip Number", typeof(string));
            TW.Columns.Add("WS Create", typeof(string));
            TW.Columns.Add("Agent", typeof(string));

            ///////////////////////////////////////////////
            int row = dtWeightSlip.Tables[0].Rows.Count;
            for (int i = 0; i < row; i++)
            {
                MAWB_id = dtWeightSlip.Tables[0].Rows[i]["mawb_id"].ToString().Trim();
                slip_id = dtWeightSlip.Tables[0].Rows[i]["slip_id"].ToString().Trim();
                MAWB = dtWeightSlip.Tables[0].Rows[i]["display_mawb"].ToString().Trim();
                HAWB = dtWeightSlip.Tables[0].Rows[i]["hawb"].ToString().Trim();



                awb_seq = dtWeightSlip.Tables[0].Rows[i]["awb_seq"].ToString().Trim();
                awb_move_seq = dtWeightSlip.Tables[0].Rows[i]["awb_move_seq"].ToString().Trim();
                partial_seq = dtWeightSlip.Tables[0].Rows[i]["partial_seq"].ToString().Trim();

                Piece = dtWeightSlip.Tables[0].Rows[i]["total_piece_count"].ToString().Trim();
                net = dtWeightSlip.Tables[0].Rows[i]["total_net"].ToString().Trim();
                carrier = dtWeightSlip.Tables[0].Rows[i]["carrier"].ToString().Trim();
                flight_number = dtWeightSlip.Tables[0].Rows[i]["flight_number"].ToString().Trim();
                flight_date = dtWeightSlip.Tables[0].Rows[i]["display_flight_date"].ToString().Trim();
                destination = dtWeightSlip.Tables[0].Rows[i]["destination"].ToString().Trim();
                NOG = dtWeightSlip.Tables[0].Rows[i]["nature_of_goods"].ToString().Trim();
                scale_id = dtWeightSlip.Tables[0].Rows[i]["scale_id"].ToString().Trim();
                slip_number = dtWeightSlip.Tables[0].Rows[i]["slip_number"].ToString().Trim();
              //  TWSC = dtWeightSlip.Tables[0].Rows[i]["create_date"].ToString().Trim();
                TWSC = Utilities.DateFormatting_dd_mm_yy_time(dtWeightSlip.Tables[0].Rows[i]["create_date"]);
                  
                agent_code = dtWeightSlip.Tables[0].Rows[i]["agent_code"].ToString().Trim();

                TW.Rows.Add(MAWB_id, slip_id, MAWB, HAWB, awb_seq, awb_move_seq, partial_seq, Piece, net, carrier, flight_number, flight_date, destination, NOG, scale_id, slip_number, TWSC, agent_code);
            }
            return TW;
        }

        private void SearchWeightSlipwithdate()
        {
            // SqlCommand cmd = new SqlCommand();

            // // Int32 rowsAffected;

            // cmd.CommandText = "usp_SearchWeightSlipWithDate";
            // cmd.CommandType = CommandType.StoredProcedure;
            // cmd.Connection = sqlCon;
            // cmd.Parameters.AddWithValue("@in_vchApplicationID", appId);
            // cmd.Parameters.AddWithValue("@in_dtFlightDate", dpkFlightDate.Value);
            // cmd.Parameters.AddWithValue("@in_vchDevice", device);
            // cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.userName);
            // cmd.Parameters.AddWithValue("@in_vchMAWB", txtPrefix.Text + "-" + txtSerial.Text);
            // cmd.Parameters.AddWithValue("@in_vchMAWBPrefix", txtPrefix.Text);
            // cmd.Parameters.AddWithValue("@in_vchMAWBSerial", txtSerial.Text);
            // cmd.Parameters.AddWithValue("@in_vchHAWB", txtHAWB.Text);
            // cmd.Parameters.AddWithValue("@in_vchCarrier", txtCarrier.Text);
            // cmd.Parameters.AddWithValue("@in_vchFlightNumber", txtFltNo.Text);
            // cmd.Parameters.AddWithValue("@in_vchAgentCode", txtAgentCode.Text);
            // cmd.Parameters.AddWithValue("@in_vchScaleID", txtScaleID.Text);
            // cmd.Parameters.AddWithValue("@in_vchSlipNumber", txtSlipNumber.Text);
            // cmd.Parameters.AddWithValue("@in_vchShipmentype", comboboxst);


            //  DateTime dateFrom = dpkFromDate.Value;
            //  string datedpkfrom = dateFrom.Date.ToString("yyyy-MM-dd");


            // ConvertDate(datedpkfrom);
            // string allfromdate = newdate + " " + txtDateFrom.Text;
            //cmd.Parameters.AddWithValue("@in_dtFromDateTime", allfromdate);



            ////DateTime myDate = DateTime.ParseExact(allfromdate, "yyyy-MM-dd HH:mm",
            ////                           System.Globalization.CultureInfo.InvariantCulture);



            // DateTime dateto = dpkToDate.Value;
            // string datedpkto = dateto.Date.ToString("yyyy-MM-dd");
            // ConvertDate(datedpkto);
            // string alltodate = newdate + " " + txtDateTo.Text;
            //cmd.Parameters.AddWithValue("@in_dtToDateTime", alltodate);

            //sqlCon.Open();

            //SqlDataAdapter da = new SqlDataAdapter(cmd);
            //dtWeightSlip = new DataSet();
            //da.Fill(dtWeightSlip, "WeightSlip");

            SearchCriteria criteria = new SearchCriteria();
            criteria.AppId = GlobalVariable.user.ApplicationID;
            criteria.Device = GlobalVariable.Device;
            criteria.UserId = GlobalVariable.user.UserID;
            if (!string.IsNullOrEmpty(txtdatetime.Text))
            {
                //criteria.FlightDate = dpkFlightDate.Value;
                criteria.FlightDate = Convert.ToDateTime(txtdatetime.Text);
                
            }
            criteria.MAWB = txtPrefix.Text + "-" + txtSerial.Text;
            criteria.MAWBPrefix = txtPrefix.Text;
            criteria.MAWBSerial = txtSerial.Text;
            criteria.HAWB = txtHAWB.Text;
            criteria.Carrier = txtCarrier.Text;
            criteria.FlightNo = txtFltNo.Text;
            criteria.AgentCode = txtAgentCode.Text;
            criteria.ScaleId = txtScaleID.Text;
            criteria.SlipNo = txtSlipNumber.Text;
            criteria.SipmentType = comboboxst;

            DateTime dateFrom = dpkFromDate.Value;
            string datedpkfrom = dateFrom.Date.ToString("yyyy-MM-dd");
            ConvertDate(datedpkfrom);
            string allfromdate = newdate + " " + txtDateFrom.Text;

            DateTime dateto = dpkToDate.Value;
            string datedpkto = dateto.Date.ToString("yyyy-MM-dd");
            ConvertDate(datedpkto);
            string alltodate = newdate + " " + txtDateTo.Text;

            dtWeightSlip = searchData.SearchWeightSlipWithDateRange(criteria, allfromdate, alltodate);

            DataTable MAWB_TB = CreateWeightSlipDisplay_TB(dtWeightSlip);
            dgvWeightSlipDisplay.DataSource = MAWB_TB;

            //dgvWeightSlipDisplay.Columns[0].Visible = false;
            //dgvWeightSlipDisplay.Columns[1].Visible = false;
            setRowNumber(dgvWeightSlipDisplay);
            if (dgvWeightSlipDisplay.RowCount > 0)
            {
                setdgvdisplay();
                dgvWeightSlipDisplay.Rows[0].Selected = true;
            }
            else
            {
                CommonMessageBox.ShowMessageWarning("Data not found");
            }
            
            //sqlCon.Close();
        }

        private void setdgvdisplay()
        {
            dgvWeightSlipDisplay.Columns[0].Visible = false;
            dgvWeightSlipDisplay.Columns[1].Visible = false;
            dgvWeightSlipDisplay.RowHeadersWidth = 60;
            dgvWeightSlipDisplay.Columns[5].Width = 70;
            dgvWeightSlipDisplay.Columns[6].Width = 50;
            dgvWeightSlipDisplay.Columns[7].Width = 50;
            dgvWeightSlipDisplay.Columns[8].Width = 50;
            dgvWeightSlipDisplay.Columns[9].Width = 50;
            dgvWeightSlipDisplay.Columns[10].Width = 50;
            dgvWeightSlipDisplay.Columns[11].Width = 70;
            dgvWeightSlipDisplay.Columns[12].Width = 50;
            dgvWeightSlipDisplay.Columns[14].Width = 70;
            dgvWeightSlipDisplay.Columns[15].Width = 70;
        }

        //ตรวจจับรูปแบบวันที่
        public void ConvertDate(string dateinput)
        {
            string dd = dateinput.Substring(8, 2);
            string mm = dateinput.Substring(5, 2);
            string yyyy = dateinput.Substring(0, 4);

            int yyyyint = Convert.ToInt32(yyyy);

            if (yyyyint > 2200)
            {
                yyyyint = yyyyint - 543;
            }

            else
            {
                //
            }

            string yyyyst = yyyyint.ToString();
            newdate = "" + yyyyst + "-" + mm + "-" + dd + "";

        }

        private void set_PHtime_to_dpk()
        {

            //เช็ค Datetime nows            
            DateTime Dateto = DateTime.Now;


            // เอาเวลาปัจจุบัน ลบ 1ชั่วโมง(เพราะจะหาค่าชั่วโมงที่แล้ว
            System.TimeSpan duration = new System.TimeSpan(0, -1, 0, 0);
            System.DateTime dateFromnow = Dateto.Add(duration);

            //เอาวันทีใส่ datepicker
            dpkFromDate.Value = dateFromnow;
            dpkToDate.Value = Dateto;

            //เอาเวลาใส่ datepicker

            //แปลง datetime now เป็น string   
            string sttimeFromnow = dateFromnow.ToString("HH:mm");
            string sttimetonow = Dateto.ToString("HH:mm");
            //เอาstring ลง textbox
            txtDateFrom.Text = sttimeFromnow;
            txtDateTo.Text = sttimetonow;

        }

        private void set_CDtime_to_dpk()
        {

            //เช็ค Datetime nows            
            DateTime Dateto = DateTime.Now;


            //เอาวันทีใส่ datepicker
            dpkFromDate.Value = Dateto;
            dpkToDate.Value = Dateto;

            //เอาเวลาใส่ datepicker

            //แปลง datetime now เป็น string   
            string sttimeFromnow = "00:00";
            string sttimetonow = Dateto.ToString("HH:mm");
            //เอาstring ลง textbox
            txtDateFrom.Text = sttimeFromnow;
            txtDateTo.Text = sttimetonow;

        }

        private void set_PDtime_to_dpk()
        {

            //เช็ค Datetime nows            
            DateTime Dateto = DateTime.Now;


            // เอาเวลาปัจจุบัน ลบ 1ชั่วโมง(เพราะจะหาค่าชั่วโมงที่แล้ว
            System.TimeSpan duration = new System.TimeSpan(-1, 0, 0, 0);
            System.DateTime dateFromnow = Dateto.Add(duration);

            //เอาวันทีใส่ datepicker
            dpkFromDate.Value = dateFromnow;
            dpkToDate.Value = dateFromnow;

            //เอาเวลาใส่ datepicker

            //แปลง datetime now เป็น string   
            string sttimeFromnow = "00:00";
            string sttimetonow = "23:59";
            //เอาstring ลง textbox
            txtDateFrom.Text = sttimeFromnow;
            txtDateTo.Text = sttimetonow;

        }


        public void validationMMHH(TextBox stfromdate)
        {

            //ดักว่าต้อง pass ก่อนหน้าถึงมีสิทธิแทนที่ค่า   stvalidationMMHH
            if (stvalidationMMHH.Equals("PASS"))
            {

                // string stfromdate = txtDateFrom.Text;
                if (stfromdate.Text.Length == 5)
                {
                    string hh = stfromdate.Text.Substring(0, 2);
                    string mm = stfromdate.Text.Substring(3, 2);
                    string center = stfromdate.Text.Substring(2, 1);


                    int inthh;
                    int intmm;

                    if (int.TryParse(hh, out inthh))
                    {
                        if (inthh >= 0 && inthh <= 23)
                        {
                            if (int.TryParse(mm, out intmm))
                            {
                                if (intmm >= 0 && intmm <= 59)
                                {
                                    if (center.Equals(":"))
                                    {
                                        stvalidationMMHH = "PASS";
                                    }
                                    else
                                    {
                                        CommonMessageBox.ShowMessageWarning("กรุณาใส่ที่แบ่งชั่วโมงและนาทีให้เป็น :  เท่านั้น");
                                        stvalidationMMHH = "NO PASS";
                                        stfromdate.Focus();
                                    }
                                }
                                else
                                {
                                    CommonMessageBox.ShowMessageWarning("กรุณานาทีให้ถูกต้อง 0-59 เท่านั้น");
                                    stvalidationMMHH = "NO PASS";
                                    stfromdate.Focus();
                                }
                            }
                            else
                            {
                                CommonMessageBox.ShowMessageWarning("กรุณาใส่นาทีตัวเลขเท่านั้น");
                                stvalidationMMHH = "NO PASS";
                                stfromdate.Focus();
                            }
                        }

                        else
                        {
                            CommonMessageBox.ShowMessageWarning("กรุณาชั่วโมงให้ถูกต้อง 0-23 เท่านั้น");
                            stvalidationMMHH = "NO PASS";
                            stfromdate.Focus();
                        }
                    }

                    else
                    {
                        CommonMessageBox.ShowMessageWarning("กรุณาใส่ชั่วโมงตัวเลขเท่านั้น");
                        stvalidationMMHH = "NO PASS";
                        stfromdate.Focus();
                    }
                }


                else
                {
                    CommonMessageBox.ShowMessageWarning("กรุณาตรวจสอบความยาวของรูปแบบการกรอกข้อมูล");
                    stvalidationMMHH = "NO PASS";
                    stfromdate.Focus();
                }

                //จบดักว่าต้อง pass ก่อนหน้าถึงมีสิทธิแทนที่ค่า   stvalidationMMHH
            }

            else
            {
                //ไม่ต้องทำอะไร
            }

        }



        private void checkcombobox()
        {
            try
            {
                if (cboConsolType.SelectedItem.ToString().Equals("CONSOL"))
                {
                    comboboxst = "CONSOL";
                }
                else if (cboConsolType.SelectedItem.ToString().Equals("NONCON"))
                {
                    comboboxst = "NONCON";
                }
                else
                {
                    comboboxst = "";
                }
            }

            catch
            {
                comboboxst = "";
            }




        }

        private void setRowNumber(DataGridView dgv)
        {
            //try
            //{
                // dgvWeight_Disp.
                //dgv.RowHeadersWidth = 100;
                //dgvWeight_Disp.Columns[-1].HeaderText = "No";

                foreach (DataGridViewRow row in dgv.Rows)
                {
                    //if (row.Index == dgv.Rows.Count)
                    //{
                    //    row.HeaderCell.Value = "";
                    //}
                    //else
                    {
                        row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
                    }
                }
            //}
            //catch
            //{

            //    // throw;
            //}

        }
        #endregion
    }
}
